package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import dao.UserDao;

public class QueryErrCodeServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3805215732561982000L;

	public QueryErrCodeServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();

		// String dateSearch = request.getParameter("dateSearch");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");

		JSONObject jsonObject = new JSONObject();
		UserDao userDao = null;
		Map<String, String> map = null;
		
		try {
			userDao = new UserDao();
			map = userDao.queryErrCode(startDate, endDate);
			
			if(map != null && map.size() > 0) {
				jsonObject.put("retCode", "1");
				jsonObject.put("errCodes", JSONObject.fromObject(map));
			}
			else {
				jsonObject.put("retCode", "0");
				jsonObject.put("desc", "无记录");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			jsonObject.put("retCode", "0");
			jsonObject.put("desc", "QueryErrCodeServlet Exception");
		} finally {
			out.print(jsonObject.toString());
			out.close();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	public void init() throws ServletException {
	}

}
